require(['../config/requireconfig'],function(config){
	//根据需要添加
	require(['jquery','common/pccommons','vendor/editable',"common/prodgen"],function($,com,editable,prodgen){
		
        com.init({headerPos:{menu:2,sub:2}});
		var pageLogic = new PageLogic($,com,prodgen);
		pageLogic.init();

	});
});
var PageLogic = function($,com,prodgen){
	return{
		init:function(){
			var _this = this;
			_this.initBrand();
			_this.initSerial();
			_this.initVol();
		},
		// 汽车品牌
		initBrand:function(){
			var _this = this;
			_this.brandName = "";
			var brandTool = {
				initEditor:function(brands){
					if(!brands){
						return;
					}
					var options = "";
					$(brands).each(function(){
						options += '<option style="cursor:pointer;" value="' + this.brandName + '">' + this.brandName + '</option>';
					});
					$('#carbrand').append(options);
					$('#carbrand').editableSelect({
			            bg_iframe : true,
						onSelect : function(list_item) {
							var tmpBrand = $("#carbrand").val();
							if(_this.brandName == tmpBrand){
								return;
							}else{
								_this.brandName = tmpBrand;
							}
							_this.initSerial();
							_this.refreshContent();
						},
						case_sensitive : false,
						items_then_scroll : 10,
						isFilter : true
					});
				}
			};
			com.ajax({
				url:'/queryBrandList.json',
				success:function(res){
					brandTool.initEditor(res.data);
				}
			});

		},
		// 车型系列
		initSerial:function(){
			var _this = this;
			var selval = $("#carbrand").val();
			var defVal = "<option value='0'>选择车型系列</option>";
			if(selval && selval != '选择汽车品牌'){
				var data = {brandName:selval};
				com.ajax({
					url : '/getProdType.json',
					data:data,
					success:function(res){
						var arr = [];
						$.each(res.data, function(idx, obj) {
							arr.push(obj.modelName);
						});
						var str = "";
						$.each(com.utils().uniqueArr(arr), function(idx, obj) {
							if(arr.hasOwnProperty(idx)){
								str += "<option value='" + obj + "'>" + obj + "</option>";
							}
						});
						$("#carSerial").html(defVal + str);
						_this.initVol("");
					}
				})
			}else{
				$("#carSerial").html("");
				$("#carSerial").html(defVal);
				_this.initVol("");
			}
			$("#carSerial").unbind("change").bind("change",function(){
				_this.initVol($(this).val());
				_this.refreshContent();
			});

		},
		// 
		initVol:function(selval){
			var _this = this;
			var defVal = "<option value='0'>选择车型排量</option>";
			if(selval && selval != "0"){
				var dicVehicle = {
					modelName : $("#carSerial").val(),
					brandName : $("#carbrand").val()
				};
				com.ajax({
					url:'/getProdDis.json',
					data:dicVehicle,
					success:function(res){
						var j = res.data;
						var dispL = [];
						$.each(res.data, function(idx, obj) {
							if(res.data.hasOwnProperty(idx)){
								dispL.push(obj);
							}
						});
						var str = "";
						$.each(com.utils().uniqueArr(dispL), function(idx, obj) {
							if(dispL.hasOwnProperty(idx)){
								if(obj){
									str +='<option value="' + obj + '">' + parsIntToNum(obj) + '</option>';
								}
							}
						});
						$("#carVol").html(defVal + str);
					}
				});
			}else{
				$("#carVol").html("");
				$("#carVol").html(defVal);
			}
			$("#carVol").unbind("change").bind("change",function(){
					_this.refreshContent();
			});
		},
		refreshContent:function() {
			var res = {};
			var _this = this;
			$(".search-con").html("");
			var brandName = $("#carbrand").val();
			if(!brandName || brandName == "选择汽车品牌"){
				return;
			}
			res.brandName = brandName;
			var modelName = $("#carSerial").val();
			if(!modelName || modelName == "0"){
				return;
			}
			res.modelName = modelName;
			var displacementL = $("#carVol").val();
			if(displacementL && displacementL != "0"){
				res.displacementL = displacementL;
			}
			com.ajax({
				url:"/getProDdet.json",
				data:res,
				success:function(resp) {
					_this.resInfo(resp.data);
				}
			})

		},
		resInfo:function(res) {
			var html = "";
			var _this = this;
			html +='<div class="search-con-item">';
			html +='  <div class="search-con-item-title">查询结果</div>';
			html +='  <div class="dk-table modelres">';
			html +='	<table>';
			html +='	  <thead>';
			html +='	  <th width="20%">品牌</th>';
			html +='	  <th width="30%">车系</th>';
			html +='	  <th width="30%">车型</th>';
			html +='	  <th width="20%">排量（L）</th>';
			html +='	  <th width="20%">车型起始日期</th>';
			html +='	  <th width="20%">车型结束日期</th>';
			html +='	  </thead>';
			html +='	  <tbody>';
			var trs = "";
			$(res).each(function(){
				trs +='	  <tr tg="' + this.vehicleNo + '">';
				trs +='		<td>' + this.brandName + '</td>';
				trs +='		<td>' + (this.modelName?this.modelName:"") + '</td>';
				trs +='		<td>' + (this.engineNo?this.engineNo:"") + '</td>';
				trs +='		<td>' + (this.displacementL?this.displacementL:"") + '</td>';
				trs +='		<td>' + (this.startDate?com.utils().formatDateYYmm(this.startDate):"") + '</td>';
				trs +='		<td>' + (this.stopDate?com.utils().formatDateYYmm(this.stopDate):"") + '</td>';
				trs +='	  </tr>';
			});
			html += trs;
			html +='	  </tbody>';
			html +='	</table>';
			html +='  </div>';
			html +='</div>';
			html +='<div class="fixprdInfo">';
			html +='</div>';
			$(".search-con").html(html);
			$(".modelres").find("tbody").find("tr").hover(function(){
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});
			$(".modelres").find("tbody").find("tr").on("click",function(){
				if($(this).hasClass("active")){
					return;
				}
				var vehicleNo = $(this).attr("tg");
				$(".modelres").find("tbody").find("tr").removeClass("active");
				$(this).addClass("active");
				if(vehicleNo){
					$(".fixprdInfo").html("");
					var obj = {vehicleNo : vehicleNo};
					com.ajax({
						url:'/getPartDet.json',
						data:obj,
						success:function(res){
							_this.fixprdInfo(res.data,vehicleNo); 
						}
					})
				}
				
			});
		},
		// 适用产品
		fixprdInfo:function(data,vehicleNo){
			var _this = this;
			//AbleProd,AbleProdSGMD,typelst
			var AbleProd = data.AbleProd;
			var AbleProdSGMD = data.AbleProdSGMD;
			var tyrelst = data.tyreList;
			var typeInfoTool= {
				genFixPrds:function(){
					var _this =this;
					_this.curLineNo = 0;
					var html = "";
					html += _this.schemaStart();
					html += _this.loadPlist();
					html += _this.loadTyreList();
					html += _this.defaultPart();
					html += _this.schemaEnd();
					return html;
				},
				loadPlist:function(){
					var _this =this;
					var arr = [];
					var idxNo = 0;
					$.each(AbleProd, function(idx, obj) {
						arr.push(obj.modelName);
					});
					var html = "";
					$.each(com.utils().uniqueArr(arr),function(idx, obj) {
						if(arr.hasOwnProperty(idx)){
							var cls = _this.rowClass();
							html +='<div class="product-list-item ' + cls + ' ">';
							html +='<span class="fontSize14 mr60 title">' + obj + '</span>';
							$(AbleProd).each(function(){
								if(this.modelName == obj){
									html += '<span class="fontSize12 mr60" tg="1" val="' + this.valveNum + '" parNo="' +  vehicleNo+ '">' + this.modelNo + '</span>'
								}
							})
              				html += '</div>';
						}
					});
					return html;
				},
				loadTyreList:function(){
					var html = "";
					var _this =this;
					if(tyrelst && tyrelst.length >0){
						var cls = _this.rowClass();
						html += '<div class="product-list-item ' + cls + ' ">';
						html +='<span class="fontSize14 mr60 title">轮胎</span>';
						var val1 = [],val2=[],val3=[];
						$(tyrelst).each(function(){
							val1.push(this.value1);
							val2.push(this.value2);
							val3.push(this.value3);
						});
						var selOpt1 = '<option value="">截面长度</option>';
						var selOpt2 = '<option value="">轮毂直径</option>';
						var selOpt3 = '<option value="">高宽比</option>';
						$.each(com.utils().uniqueArr(val1),function(idx,obj){
							if(obj){
								selOpt1 += '<option value="' + obj + '">'+ obj+ '</option>';	
							}
						});
						$.each(com.utils().uniqueArr(val2),function(idx,obj){
							if(obj){
								selOpt2 += '<option value="' + obj + '">'+ obj+ '</option>';
							}
						});
						$.each(com.utils().uniqueArr(val3),function(idx,obj){
							if(obj){
								selOpt3 += '<option value="' + obj + '">'+ obj+ '</option>';
							}
						});
						html += '<span class="fontSize12 mr60 typeitem"><select id="sel1">' + selOpt1 + '</select></span>';
						html += '<span class="fontSize12 mr60 typeitem"><select id="sel3" >' + selOpt3 + '</select></span>';
						html += '<span class="fontSize12 mr60 typeitem"><select id="sel2">' +  selOpt2+ '</select></span>';
						html += '</div>';
					}

					return html;
				},
				defaultPart:function(){
					var _this = this;
					var cls = _this.rowClass();
					var html = "";
					html += '<div class="product-list-item ' + cls + ' ">';
					html +='<span class="fontSize14 mr60 title">油液及化学品</span>';
					if(AbleProdSGMD){
						$(AbleProdSGMD).each(function(){
							if(this.valveNum == "10H"){
								html += '<span class="fontSize12 mr60" tg="1" val="10H" parNo="' +  vehicleNo+ '" >自动变速箱油</span>'
							}
							else if(this.valveNum == "10D"){
								html += '<span class="fontSize12 mr60" tg="1" val="10D" parNo="' +  vehicleNo+ '" >汽机油</span>'
							}
						});
						html += '<span class="fontSize12 mr60" tg="2" val="10C" parNo="' +  vehicleNo+ '">防冻液</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10E" parNo="' +  vehicleNo+ '">制动液</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10F" parNo="' +  vehicleNo+ '">转向助力液</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10B" parNo="' +  vehicleNo+ '">齿轮油</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10J" parNo="' +  vehicleNo+ '">冷媒</span>';
					}else{
						html += '<span class="fontSize12 mr60" tg="2" val="10C" parNo="' +  vehicleNo+ '">防冻液</span>';
						html += '<span class="fontSize12 mr60" tg="1" val="10D" parNo="' +  vehicleNo+ '">汽机油</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10E" parNo="' +  vehicleNo+ '">制动液</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10F" parNo="' +  vehicleNo+ '">转向助力液</span>';
						html += '<span class="fontSize12 mr60" tg="1" val="10H" parNo="' +  vehicleNo+ '" >自动变速箱油</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10B" parNo="' +  vehicleNo+ '">齿轮油</span>';
						html += '<span class="fontSize12 mr60" tg="2" val="10J" parNo="' +  vehicleNo+ '">冷媒</span>';
					}
					html += '</div>';
					return html;
				},
				rowClass:function(){
					var _this = this;
					_this.curLineNo += 1;
					var cls = "";
					if(_this.curLineNo % 2 == 1){
						cls = "grey-bg";
					}
					return cls;
				},
				schemaStart:function(){
					var html = "";
					html +='<div class="search-con-item">';
					html +='	<div class="search-con-item-title">适用产品</div>';
					html +='	<div class="product-list">';
					return html;
				},
				schemaEnd:function(){
					var html = "";
					html +='	</div>';
					html +='</div>';
					return html;
				}
			}

			var html = typeInfoTool.genFixPrds() ;
			html += '<div class="partInfo"></div><div class="search-con-res"></div>';
			$(".fixprdInfo").html(html);
			$(".fixprdInfo").find(".fontSize12").hover(function() {
				$(this).addClass("hover");
			},function(){
				$(this).removeClass("hover");
			});
			$(".fixprdInfo").find(".fontSize12").on("click",function(){
				if($(this).hasClass("active")){
					return;
				}
				var tg = $(this).attr("tg");
				if(!tg){
					return;
				}
				$(".partInfo").html("");
				$(".search-con-res").html("");
				$(".fontSize12").removeClass("active");
				$(this).addClass("active");
				bindSelect();
				var val = $(this).attr("val");
				var parNo = $(this).attr("parNo");
				var obj = {};
				var url = "";
				if(tg == "1"){
					// 其他的物品
					url = '/gettyreList.json';
					obj.vehicleNo = parNo;
					obj.transType = val;
				}else{ 
					//化学
					url = "/prodTeamList1.json";
					obj.sublineNo = val;
				}
				com.ajax({
					url:url,
					data:obj,
					success:function(res){
						prodgen.genpartInfo(res.data,$(".partInfo"),_this.setPrdDetailInfo);
					}
				});
			});
			bindSelect();
			function bindSelect(){
				$(".fixprdInfo").find(".fontSize12").find("select").unbind("change").val("");
				$(".fixprdInfo").find(".fontSize12").find("select").on("change",function(){
					$(".fixprdInfo").find(".fontSize12").removeClass("active");
					$(".partInfo").html("");
					$(".search-con-res").html("");
					var obj = {};
					obj.reserve1 = $("#sel1").val()||'';
					obj.reserve2 = $("#sel2").val()||'';
					obj.reserve3 = $("#sel3").val()||'';
					obj.vehicleNo = vehicleNo;
					com.ajax({
						url:"/tyreList.json",
						data:obj,
						success:function(res){
							prodgen.genpartInfo(res.data,$(".partInfo"),_this.setPrdDetailInfo);
						}
					});
				});	
			}
		},
		setPrdDetailInfo:function(param){
			var _this = this;
			$(".search-con-res").html("");
			if(param){
				var data = {vehicleNo:param};
				com.ajax({
					url:"/getPartNumber2.json",
					data:data,
					success:function(res){
						prodgen.prdRemark($(".search-con-res"),res.data.desList,res.data.desList1,res.data.lj);
						prodgen.paramInfo($(".search-con-res"),res.data.partList);
						prodgen.carInfo($(".search-con-res"),res.data.selectVehicleNoByPart);			
					}
				})
			}	
		}
	}
}

function parsIntToNum(num) {
	if (parseInt(num) == num) {
		num += '';
	}
	return num;
}
